package com.example.jdbcdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger2
@EnableWebMvc
public class SwaggerConfig {

    //配置Swagger的Docket的Bean
    @Bean
    public Docket docketDefault(Environment environment){

        //设置要用到的几个环境，这里默认dev
        Profiles profiles=Profiles.of("dev");
        //判断是否位于自己设置的环境中
        boolean flag = environment.acceptsProfiles(profiles);//是dev就为true


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
                //selectors配置扫描哪里的接口basepackage指定我的包，简单配置
                .apis(RequestHandlerSelectors.basePackage("com.example.jdbcdemo.controller"))
                .build();
    } //enable(false)就是关闭Swagger2，正式发布时不要使用Swagger2.


    //配置信息,http://localhost:8081/swagger-ui/index.html
    //闲的没事干写的0.0
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "SwaggerAPI文档",
                "进销存demo",
                "v1.0",
                "https://gitee.com/",
                new Contact("许凡楠","https://gitee.com/","x1255993080@163.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

    //若要协同开发，可设计多个分组，相关配置可仿照上面的docketDefault的实现
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

}
